// myself/my-active/sign.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        style: app.globalData.style,
        details: {},
        timer: {},
        canSign: true,
        distance: '',
        visible1: false,
        visible2: false,
        visible3: false,
        modalShow: false,
        bookingTimes: [
          {
            format1: '2025.10.28',
            format2: "09:00~12:00",
            signStatus: '10'
          },
          {
            format1: '2025.10.28',
            format2: '14:00-17:00',
            signStatus: '30'
          },
          {
            format1: '2025.10.29',
            format2: '14:00-17:00',
            signStatus: '00'
          }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let details = JSON.parse(decodeURIComponent(options.details));
        for (let i = 0; i < details.activePartList.length; i++) {
            details.activePartList[i].now = new Date().getTime();
            if (details.activePartList[i].signTime) {
                details.activePartList[i].signTimeStr = formatDate.formatTime(details.activePartList[i].signTime, 'yyyy-MM-dd HH:mm')
            }
        }
        let timer = this.getNearestTime(details.activePartList);
        this.setData({
            details,
            timer
        })
        wx.getLocation({
            type: 'gcj02',
            success: res => {
                const latitude = res.latitude
                const longitude = res.longitude
                console.log(latitude, longitude)
                console.log(details.siteDO.lat, details.siteDO.lng)
                let distance = app.getDistance(latitude, longitude, details.siteDO.lat, details.siteDO.lng);
                console.log(distance)
                if (distance > app.globalData.distance) {
                    this.setData({
                        canSign: false
                    })
                }
            }
        })
    },

    getNearestTime(dataArray) {
        const now = new Date().getTime(); // 当前时间
        let nearest = dataArray[0]; // 假设第一个是最接近的

        dataArray.forEach(item => {
            const diff = Math.abs(item.time - now); // 计算时间差的绝对值
            if (diff < Math.abs(nearest.time - now)) {
                nearest = item; // 更新最接近的项
            }
        });

        return nearest; // 返回最接近的数据项
    },

    selectedTimer(e) {
        const timer = e.currentTarget.dataset.item;
        this.setData({
            timer
        })
    },

    signTap() {
      const modal = this.selectComponent('#myModal');
      modal.show({
        message: '签到失败',
        reason: "当前不在活动地点范围"
      })
        // this.setData({
        //   modalShow: true
        // })
        // network.postJson(api.activePartUpdate, {
        //     id: this.data.timer.id,
        //     activeId: this.data.timer.activeId,
        //     userId: app.globalData.userInfo.id,
        //     timerId: this.data.timer.timerId,
        //     status: 1
        // }).then(res => {
        //     this.data.timer.status = 1;
        //     this.data.timer.signTimeStr = formatDate.formatTime(new Date(), 'yyyy-MM-dd HH:mm')
        //     this.setData({
        //         timer: this.data.timer
        //     })
        //     wx.showToast({
        //         icon: 'none',
        //         title: '签到成功'
        //     })
        // })
    },

    onVisible1Change(e) {
        this.setData({
            visible1: e.detail.visible
        });
    },

    onVisible2Change(e) {
        this.setData({
            visible2: e.detail.visible
        });
    },

    onVisible3Change(e) {
        this.setData({
            visible3: e.detail.visible
        });
    },

    close1Tap() {
        this.setData({
            visible1: false
        });
    },

    close2Tap() {
        this.setData({
            visible2: false
        });
    },

    close3Tap() {
        this.setData({
            visible3: false
        });
    }
})